﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using YwangHRMSys.DBUtility;
using YwangHRMSys.Model;


namespace YwangHRMSys.DAL
    {
        public class AdminPermissionDal
            {
                #region 开始生成tomodel提高代码的重用性

                /// 开始生成tomodel提高代码的重用性
                private AdminPermissionModel ToModel( DataRow row )
                {
                    var model = new AdminPermissionModel();
                    model.Id = (Guid) SqlHelper.FromDbValue(row["Id"]);
                    model.UserName = (String) SqlHelper.FromDbValue(row["UserName"]);
                    model.Permission = (Int32) SqlHelper.FromDbValue(row["Permission"]);
                    return model;
                } //tomodel的结束。

                #endregion

                #region listAll开始

                //查询所有AdminPermission的数据列表
                public IEnumerable <AdminPermissionModel> ListAll()
                {
                    var list = new List <AdminPermissionModel>();
                    DataTable table = SqlHelper.ExecuteDataTable("Select Id,UserName,Permission FROM  AdminPermission");
                    foreach (DataRow row in table.Rows)
                        {
                            AdminPermissionModel model = ToModel(row);
                            list.Add(model);
                        }
                    return list;
                } //listAllAdminPermission结束

                #endregion

                #region  根据ID来查询AdminPermission的信息

                public AdminPermissionModel GetByAdminPermissionID( Guid Id )
                {
                    DataTable table = SqlHelper.ExecuteDataTable
                            (
                                    " SELECT Id,UserName,Permission FROM  AdminPermission  WHERE  Id = @Id",
                                    new SqlParameter("@Id", Id));
                    if (table.Rows.Count <= 0)
                        {
                            return null;
                        }
                    if (table.Rows.Count == 1)
                        {
                            return ToModel(table.Rows[0]);
                        }
                    throw new Exception();
                }

                #endregion

                #region  根据ID修改AdminPermission的信息

                public void UpdateAdminPermission( Guid id ,string UserName )
                {
                    SqlHelper.ExecuteNonQuery
                            (
                                    " UPDATE  AdminPermission SET  UserName = @UserName,Permission =0  WHERE  Id = @Id",
                                    new SqlParameter("@Id", id),
                                    new SqlParameter("@UserName",UserName)
                                    );
                }

                #endregion

                #region 添加AdminPermission信息

                // 添加一条AdminPermission 的信息
                public void InsertAdminPermission( AdminPermissionModel model )
                {
                    SqlHelper.ExecuteNonQuery
                            (
                                    "INSERT INTO AdminPermission  (Id,UserName,Permission  ) values (@Id,@UserName,@Permission)",
                                    new SqlParameter("@Id", model.Id),
                                    new SqlParameter("@UserName", model.UserName),
                                    new SqlParameter("@Permission", model.Permission));

                }

                #endregion

                #region 删除用户

                /// <summary>
                ///     删除用户
                /// </summary>
                /// <param name="id"></param>
                public void DeleteById( Guid id )
                {
                    SqlHelper.ExecuteNonQuery
                            (
                                    "DELETE FROM [YwangHrSys].[dbo].[AdminPermission]WHERE  Id =@Id",
                                    new SqlParameter("@Id", id));
                }

                #endregion

                //根据ID来查询AdminPermission的信息

                //根据ID修改AdminPermission的信息
            }
    }